home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / lang / float~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  5.6 KB  |  207 lines

  1. /*
  2.  * @(#)Float.java    1.30 95/11/13  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. /**
  23.  * The Float class provides an object wrapper for Float data values, and serves as
  24.  * a place for float-oriented operations.  A wrapper is useful because most of Java's
  25.  * utility classes require the use of objects.  Since floats are not objects in 
  26.  * Java, they need to be "wrapped" in a Float instance.
  27.  * @version     1.30, 11/13/95
  28.  * @author    Lee Boynton
  29.  * @author    Arthur van Hoff
  30.  */
  31. public final
  32. class Float extends Number {
  33.     /**
  34.      * Positive infinity.
  35.      */
  36.     public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
  37.  
  38.     /**
  39.      * Negative infinity.
  40.      */
  41.     public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
  42.  
  43.     /** 
  44.      * Not-a-Number. <em>Note: is not equal to anything, including
  45.      * itself</em>
  46.      */
  47.     public static final float NaN = 0.0f / 0.0f;
  48.  
  49.  
  50.     /**
  51.      * The maximum value a float can have.  The largest maximum value possible is  
  52.      * 3.40282346638528860e+38.
  53.      */
  54.     public static final float MAX_VALUE = 3.40282346638528860e+38f;
  55.  
  56.     /**
  57.      * The minimum value a float can have.  The lowest minimum value possible is 
  58.      * 1.40129846432481707e-45.
  59.      */
  60.     public static final float MIN_VALUE = 1.40129846432481707e-45f;
  61.  
  62.     /**
  63.      * Returns a String representation for the specified float value.
  64.      * @param f    the float to be converted
  65.      */
  66.     public static native String toString(float f);
  67.  
  68.     /**
  69.      * Returns the floating point value represented by the specified String.
  70.      * @param s        the String to be parsed
  71.      * @exception    NumberFormatException If the String does not contain a parsable 
  72.      * Float.
  73.      */
  74.     public static native Float valueOf(String s) throws NumberFormatException;
  75.  
  76.     /**
  77.      * Returns true if the specified number is the special Not-a-Number (NaN) value.
  78.      * @param v    the value to be tested
  79.      */
  80.     static public boolean isNaN(float v) {
  81.     return (v != v);
  82.     }
  83.  
  84.     /**
  85.      * Returns true if the specified number is infinitely large in magnitude.
  86.      * @param v    the value to be tested
  87.      */
  88.     static public boolean isInfinite(float v) {
  89.     return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
  90.     }
  91.  
  92.     /**
  93.      * The value of the Float.
  94.      */
  95.     private float value;
  96.  
  97.     /**
  98.      * Constructs a Float wrapper for the specified float value.
  99.      * @param value the value of the Float
  100.      */
  101.     public Float(float value) {
  102.     this.value = value;
  103.     }
  104.  
  105.     /**
  106.      * Constructs a Float wrapper for the specified double value.
  107.      * @param value the value of the Float
  108.      */
  109.     public Float(double value) {
  110.     this.value = (float)value;
  111.     }
  112.  
  113.     /**
  114.      * Constructs a Float object initialized to the value specified by the
  115.      * String parameter. 
  116.      * @param s        the String to be converted to a Float
  117.      * @exception    NumberFormatException If the String does not contain a parsable number.
  118.      */
  119.     public Float(String s) throws NumberFormatException {
  120.     // REMIND: this is inefficient
  121.     this(valueOf(s).floatValue());
  122.     }
  123.  
  124.     /**
  125.      * Returns true if this Float value is Not-a-Number (NaN).
  126.      */
  127.     public boolean isNaN() {
  128.     return isNaN(value);
  129.     }
  130.  
  131.     /**
  132.      * Returns true if this Float value is infinitely large in magnitude.
  133.      */
  134.     public boolean isInfinite() {
  135.     return isInfinite(value);
  136.     }
  137.  
  138.     /**
  139.      * Returns a String representation of this Float object.
  140.      */
  141.     public String toString() {
  142.     return String.valueOf(value);
  143.     }
  144.  
  145.     /**
  146.      * Returns the integer value of this Float (by casting to an int).
  147.      */
  148.     public int intValue() {
  149.     return (int)value;
  150.     }
  151.  
  152.     /**
  153.      * Returns the long value of this Float (by casting to a long).
  154.      */
  155.     public long longValue() {
  156.     return (long)value;
  157.     }
  158.  
  159.     /**
  160.      * Returns the float value of this Float object.
  161.      */
  162.     public float floatValue() {
  163.     return value;
  164.     }
  165.  
  166.     /**
  167.      * Returns the double value of this Float.
  168.      */
  169.     public double doubleValue() {
  170.     return (double)value;
  171.     }
  172.  
  173.     /**
  174.      * Returns a hashcode for this Float.
  175.      */
  176.     public int hashCode() {
  177.     return (int)value;
  178.     }
  179.  
  180.     /**
  181.      * Compares this object against some other object.
  182.      * <p>
  183.      * <em>Note: To be useful in hashtables this method
  184.      * considers two Nan floating point values to be equal. This
  185.      * is not according to IEEE specification</em>
  186.      *
  187.      * @param obj        the object to compare with
  188.      * @return         true if the objects are the same; false otherwise.
  189.      */
  190.     public boolean equals(Object obj) {
  191.     return (obj != null)
  192.            && (obj instanceof Float) 
  193.            && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
  194.     }
  195.  
  196.     /**
  197.      * Returns the bit represention of a single-float value
  198.      */
  199.     public static native int floatToIntBits(float value);
  200.  
  201.     /**
  202.      * Returns the single-float corresponding to a given bit represention.
  203.      */
  204.     public static native float intBitsToFloat(int bits);
  205.  
  206. }
  207.